package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OrderDetailsContract;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.presenter.OrderDetailsPresenter;
import com.shd.edanpei.ui.view.AuditDataView;
import com.shd.edanpei.ui.view.CarDataView;
import com.shd.edanpei.ui.view.CustomerDataView;
import com.shd.edanpei.ui.view.CustomerNeedView;
import com.shd.edanpei.ui.view.EnterpriseDataView;
import com.shd.edanpei.ui.view.FileDataView;
import com.shd.edanpei.ui.view.FilePersonalDataView;
import com.shd.edanpei.ui.view.HouseDataView;
import com.shd.edanpei.ui.view.OrderDataView;
import com.shd.edanpei.ui.view.OrderInfoView;
import com.shd.edanpei.ui.view.PersonalDataView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 共享订单详情
 */

public class ShareOrderDetailsActivity extends BaseActivity<OrderDetailsPresenter> implements OrderDetailsContract.OrderDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.tv_cancel)
    View cancel;
    @BindView(R.id.tv_pay_service)
    View payService;
    @BindView(R.id.tv_unsure)
    View unSure;
    @BindView(R.id.tv_sure)
    View sure;
    @BindView(R.id.tv_remove)
    View remove;
    @BindView(R.id.tv_goCreate)
    View goCreate;
    @BindView(R.id.tv_first)
    View first;
    @BindView(R.id.tv_end)
    View end;

    private OrderDetailsModel model;
    private int index;
    private int orderID;


    @Override
    protected int setLayout() {
        return R.layout.activity_shareorderdetails;
    }

    @Override
    protected OrderDetailsPresenter bindPresenter() {
        return new OrderDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("订单详情");
        Bundle extras = getIntent().getExtras();
        orderID = extras.getInt(Constant.ORDER_ID);
        index = extras.getInt(Constant.INDEX);
        mPresenter.requetData(orderID);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void onClose() {
        finish();
    }


    @Override
    public void setData(final OrderDetailsModel model) {
        this.model = model;
        container.addView(new OrderDataView(this, model));
        container.addView(new AuditDataView(this, model, Constant.SHARE_ORDER));
        container.addView(new CustomerNeedView(this, model));
        container.addView(new CustomerDataView(this, model));
        if (model.getBaseInfo().getOrderType() == OrderTypeEnum.car.getKey()) {
            container.addView(new CarDataView(this, model));
            container.addView(new FileDataView(this, model.getImgInfo()));
        } else if (model.getBaseInfo().getOrderType() == OrderTypeEnum.house.getKey()) {
            container.addView(new HouseDataView(this, model));
            container.addView(new FileDataView(this, model.getImgInfo()));
        }else if(model.getBaseInfo().getOrderType()==OrderTypeEnum.personal.getKey()){
            container.addView(new PersonalDataView(this, model));
            container.addView(new FilePersonalDataView(this, model.getImgInfo()));
        }else if(model.getBaseInfo().getOrderType()==OrderTypeEnum.enterprise.getKey()){
            container.addView(new EnterpriseDataView(this, model));
            container.addView(new FileDataView(this, model.getImgInfo()));
        }

        container.addView(new OrderInfoView(this, model));
        initButton();
        first.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_TYPE, model.getBaseInfo().getOrderType());
                bundle.putInt(Constant.TAG, 0);
                startActivity(SchemeActivity.class, bundle);
            }
        });
        end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_TYPE, model.getBaseInfo().getOrderType());
                bundle.putInt(Constant.TAG, 1);
                startActivity(SchemeActivity.class, bundle);
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_TYPE, model.getBaseInfo().getOrderType());
                startActivity(SureOrderActivity.class, bundle);

            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.bankUndo(model.getBaseInfo().getOrderId(), model.getBaseInfo().getOrderType(), index, model.getBaseInfo().isPayDeposit());
            }
        });
        remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.bankRemove(orderID, model.getBaseInfo().getOrderType(), index);
            }
        });
    }

    private void initButton() {
        cancel.setVisibility(View.GONE);
        first.setVisibility(View.GONE);
        end.setVisibility(View.GONE);
        unSure.setVisibility(View.GONE);
        sure.setVisibility(View.GONE);
        int status = model.getBaseInfo().getManagerStatus();
        if (status == StatusEnum.first.getStatus()) {
            //待初审
            cancel.setVisibility(View.VISIBLE);
            first.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.last.getStatus()) {
            //待终审
            end.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitService.getStatus()) {
            //已终审，未支付服务费
            unSure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitLoan.getStatus()) {
            //未放款
            sure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitfirm.getStatus()) {
            //已放款

        } else if (status == StatusEnum.success.getStatus()) {
            //交易成功

        } else if (status == StatusEnum.error.getStatus()) {
            //订单超时，无人接单
        } else if (status == StatusEnum.cancel.getStatus()) {
            //未支付，撤销订单
            remove.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.refuse.getStatus()) {
            //被拒绝
            remove.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitRepay.getStatus()) {
            //已支付，撤销订单,退款中

        } else if (status == StatusEnum.repaySuccess.getStatus()) {
            //退款成功
        }

    }

    //刷新列表
    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        model.getBaseInfo().setManagerStatus(bean.getOrderStatus());
        initButton();
    }

    @Override
    protected void onDestroy() {
        container.removeAllViews();
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

}

